package com.sinkingsquare.web.core;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

public abstract class AbstractScriptController extends AbstractBaseController {

	public final ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		 String script = getScript(request, response);
	        response.setContentType("application/x-javascript;charset=UTF-8");
	        int cache = getCacheTime(request, response);
	        if(cache<=0)
	            response.setHeader("Cache-Control", "no-cache");
	        else {
	            response.setHeader("Cache-Control", "max-age=" + cache);
	            response.addDateHeader("Expires", System.currentTimeMillis() + 1000 * cache);
	        }
	        PrintWriter writer = response.getWriter();
	        writer.write(script);
	        writer.flush();
	        return null;
	}

	public abstract int getCacheTime(HttpServletRequest request,
			HttpServletResponse response);

	public abstract String getScript(HttpServletRequest request,
			HttpServletResponse response) throws Exception;

}
